/**
 * 2025/01/01 - 2025/12/31
 */
package com.snail.server.database.engine.ddl.index.manager;

import com.snail.server.database.engine.ddl.index.dto.IndexAddDTO;
import com.snail.server.database.engine.ddl.index.dto.IndexFieldDTO;
import com.snail.server.database.engine.ddl.index.enums.IndexFieldOrderByEnum;
import com.snail.server.database.engine.ddl.index.enums.IndexTypeEnum;
import com.snail.server.database.engine.ddl.index.vo.IndexVO;
import com.snail.server.database.engine.ddl.model.index.IndexMo;
import com.snail.starter.utils.lambda.GenericEntityBuilder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zgadmin
 * @date 2025/8/29 14:33
 */
@Component
public class IndexManager {

    public Long create(Long datasourceId, Long tableId, IndexAddDTO indexCreate) {
        return 0L;
    }

    public boolean alter(Long datasourceId, Long tableId, Long indexId) {
        return false;
    }

    public IndexVO queryById(Long datasourceId, Long tableId, Long indexId) {
        return null;
    }

    public boolean delete(Long datasourceId, Long tableId, Long indexId) {
        return false;
    }

    public static List<IndexMo> toIndexMo(List<IndexAddDTO> indexes) {
        if (null == indexes || indexes.isEmpty()) {
            return new ArrayList<>();
        }
        return indexes.stream().map(IndexManager::toIndexMo).distinct().collect(Collectors.toList());
    }

    public static IndexMo toIndexMo(IndexAddDTO indexAdd) {
        return GenericEntityBuilder.of(IndexMo::new)
                .withSupplier(IndexMo::setName, indexAdd::getName)
                .with(IndexMo::setType, IndexTypeEnum.getEnum(indexAdd.getType()))
                .with(IndexMo::setIndexFields, toIndexFieldMo(indexAdd.getIndexFields()))
                .withSupplier(IndexMo::setDescription, indexAdd::getDescription)
                .build();
    }

    public static List<IndexMo.IndexField> toIndexFieldMo(List<IndexFieldDTO> indexFields) {
        if (null == indexFields || indexFields.isEmpty()) {
           return new ArrayList<>();
        }
        return indexFields.stream().map(IndexManager::toIndexFieldMo)
                .distinct()
                .collect(Collectors.toList());

    }

    public static IndexMo.IndexField toIndexFieldMo(IndexFieldDTO indexField) {
        return GenericEntityBuilder.of(IndexMo.IndexField::new)
                .withSupplier(IndexMo.IndexField::setFieldName, indexField::getFieldName)
                .with(IndexMo.IndexField::setOrderBy, IndexFieldOrderByEnum.getEnum(indexField.getOrderBy()))
                .build();

    }
}
